
package com.wyzz.hr.action;

import java.io.File;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.BaseDownloadAction;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.IEmpContractBo;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.pojo.ContractType;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Empcontract;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.Pager;

public class EmpContract extends BaseDownloadAction {

	public EmpContract() {
		page = new Pager();
		outputIoName = "OEmpContract";
		searchOrExport = null;
	}

	public String list() throws Exception {
		getDrillDownList();
		contractList = searchContract();
		if ("export".equals(searchOrExport))
			return downloadToExcel(contractList, outputIoName, outmatchModelId,
					"employee");
		else
			return "success";
	}

	private List searchContract() {
		DetachedCriteria dcCon = searchConWithEmp_DC();
		addCriteria(dcCon);
		IEmpContractBo empContractBo = (IEmpContractBo) getBean("empContractBo");
		return empContractBo.findContract(dcCon, page, searchOrExport);
	}

	private DetachedCriteria searchConWithEmp_DC() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empcontract.class);
		dc.setFetchMode(Empcontract.PROP_EMPLOYEE, FetchMode.JOIN);
		dc.createAlias(Empcontract.PROP_EMPLOYEE, "emp", 1);
		dc.createAlias(Empcontract.PROP_CONTRACT_TYPE, "contractType", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), "empDept", 1);
		if ("export".equals(searchOrExport)) {
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_PB_NO).toString(),
					Employee.PROP_EMP_PB_NO, 1);
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_LOCATION_NO).toString(),
					Employee.PROP_EMP_LOCATION_NO, 1);
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_TYPE).toString(), Employee.PROP_EMP_TYPE,
					1);
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_BENEFIT_TYPE).toString(),
					Employee.PROP_EMP_BENEFIT_TYPE, 1);
		}
		return dc;
	}

	public void addCriteria(DetachedCriteria dc) {
		if ("SUB".equals(super.authorityCondition))
			addSubDC(dc, "emp", 1);
		addOrders(dc, page, new String[] {
				(new StringBuilder()).append(Empcontract.PROP_ECT_START_DATE)
						.append("-down").toString(), "emp.empDistinctNo-up" });
		BaseCrit.addEmpDC(dc, "emp", employeeId);
		BaseCrit.addDC(dc, Empcontract.PROP_CONTRACT_TYPE, "id",
				new Object[] { new ContractType(empTypeId) });
		BaseCrit.addDCDateRange(dc, Empcontract.PROP_ECT_START_DATE,
				ectStartDate, ectStartDateTo);
		BaseCrit.addDeptDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), (new StringBuilder())
				.append("emp.").append(Employee.PROP_EMP_PB_NO).toString(),
				null, new Department(departmentId));
		BaseCrit.addDC(dc, Empcontract.PROP_ETC_EXPIRE, "eq",
				new Integer[] { etcExpire });
		BaseCrit.addDCDateRange(dc, Empcontract.PROP_ECT_END_DATE, ectEndDate,
				ectEndDateTo);
		BaseCrit.addDC(dc, Empcontract.PROP_ECT_NO, "like",
				new String[] { ectNo });
		BaseCrit.addDC(dc, Empcontract.PROP_ECT_STATUS, "eq",
				new String[] { ectStatus });
		BaseCrit.addDC(dc, Empcontract.PROP_ECT_COMMENTS, "like",
				new String[] { ectComments });
	}

	private void getDrillDownList() {
		deptList = getDrillDown("Department", new String[0]);
		empTypeList = getDrillDown("ContractType", new String[0]);
	}

	public String addContract() throws Exception {
		IEmployeeBo empBo;
		Employee employee;
		IEmpContractBo empContractBo;
		Empcontract empcontract;
		if (StringUtils.isEmpty(employeeId)) {
			addErrorInfo("\u9519\u8BEF\u7684\u8BF7\u6C42\u53C2\u6570\uFF01");
			return "error";
		}
		if ("SUB".equals(super.authorityCondition) && !checkAuth(employeeId)) {
			addActionError("\u60A8\u6CA1\u6709\u65B0\u589E\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		empBo = (IEmployeeBo) getBean("empBo");
		String fetch[] = { "contract" };
		employee = empBo.loadEmp(employeeId, fetch);
		if (employee.getContract() != null
				&& StringUtils.isNotEmpty(employee.getContract().getEctId()))
			return continueContract();
		empContractBo = (IEmpContractBo) getBean("empContractBo");
		empcontract = new Empcontract();
		if (fileFileName != null && !"".equals(fileFileName)) {
			String UploadResult;
			String pathConfig = "sys.profile.file.path";
			String typeConfig = "sys.profile.file.type";
			String lengthConfig = "sys.profile.file.length";
			String postfix = fileFileName.substring(fileFileName
					.lastIndexOf("."));
			fileFileName = (new StringBuilder()).append(UUID.randomUUID())
					.append(postfix).toString();
			UploadResult = FileOperate.buildFile(file, pathConfig, typeConfig,
					lengthConfig, new String[] { fileFileName });
			if ("property".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u4E0A\u4F20\u9519\u8BEF\uFF01");
				return "error";
			}
			if ("fileExtendNameError".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u540E\u7F00\u540D\u4E0D\u5408\u6CD5\uFF01");
				return "error";
			}
			try {
				empcontract.setEctAttatchment(fileFileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		String currentEmpNo = getCurrentEmpNo();
		empcontract.setEctNo(ectNo);
		empcontract.setEctStartDate(ectStartDate);
		empcontract.setEctEndDate(ectEndDate);
		empcontract.setEtcExpire(etcExpire);
		empcontract.setEctComments(ectComments);
		empcontract.setEctStatus(ectStatus);
		empcontract.setEmployee(new Employee(employeeId));
		empcontract.setContractType(new ContractType(empTypeId));
		empcontract.setEctCreateBy(currentEmpNo);
		empcontract.setEctCreateDate(new Date());
		empcontract.setEctLastChangeBy(currentEmpNo);
		empcontract.setEctLastChangeTime(new Date());
		String error = empContractBo.insert(empcontract);
		if (StringUtils.isNotEmpty(error)) {
			addActionError(error);
			return "input";
		} else {
			empBo.updateEmp(employee.getId(), getCurrentEmpNo(), empcontract
					.getEctId());
			addActionMessage("\u65B0\u589E\u5458\u5DE5\u5408\u540C\u6210\u529F\u3002");
			return "success";
		}
	}

	public String continueContract() throws Exception {
		IEmployeeBo empBo;
		Employee employee;
		Empcontract empcontract;
		if (StringUtils.isEmpty(employeeId)) {
			addErrorInfo("\u9519\u8BEF\u7684\u8BF7\u6C42\u53C2\u6570\uFF01");
			return "error";
		}
		if ("SUB".equals(super.authorityCondition) && !checkAuth(employeeId)) {
			addActionError("\u60A8\u6CA1\u6709\u65B0\u589E\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		empBo = (IEmployeeBo) getBean("empBo");
		String fetch[] = { "contract" };
		employee = empBo.loadEmp(employeeId, fetch);
		if (employee.getContract() == null
				|| StringUtils.isEmpty(employee.getContract().getEctId())) {
			addErrorInfo("\u5458\u5DE5\u65E0\u5408\u540C\uFF0C\u65E0\u6CD5\u8FDB\u884C\u7EED\u7B7E\u64CD\u4F5C\uFF01");
			return "error";
		}
		empcontract = new Empcontract();
		if (fileFileName != null && !"".equals(fileFileName)) {
			String UploadResult;
			String pathConfig = "sys.profile.file.path";
			String typeConfig = "sys.profile.file.type";
			String lengthConfig = "sys.profile.file.length";
			String postfix = fileFileName.substring(fileFileName
					.lastIndexOf("."));
			fileFileName = (new StringBuilder()).append(UUID.randomUUID())
					.append(postfix).toString();
			UploadResult = FileOperate.buildFile(file, pathConfig, typeConfig,
					lengthConfig, new String[] { fileFileName });
			if ("property".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u4E0A\u4F20\u9519\u8BEF\uFF01");
				return "error";
			}
			if ("fileExtendNameError".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u540E\u7F00\u540D\u4E0D\u5408\u6CD5\uFF01");
				return "error";
			}
			try {
				empcontract.setEctAttatchment(fileFileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		String currentEmpNo = getCurrentEmpNo();
		empcontract.setEctNo(ectNo);
		empcontract.setEctStartDate(ectStartDate);
		empcontract.setEctEndDate(ectEndDate);
		empcontract.setEtcExpire(etcExpire);
		empcontract.setEctComments(ectComments);
		empcontract.setEctStatus(ectStatus);
		empcontract.setEmployee(new Employee(employeeId));
		empcontract.setContractType(new ContractType(empTypeId));
		empcontract.setEctCreateBy(currentEmpNo);
		empcontract.setEctCreateDate(new Date());
		empcontract.setEctLastChangeBy(currentEmpNo);
		empcontract.setEctLastChangeTime(new Date());
		IEmpContractBo empContractBo = (IEmpContractBo) getBean("empContractBo");
		String error = empContractBo.insert(empcontract);
		if (StringUtils.isNotEmpty(error)) {
			addActionError(error);
			return "input";
		} else {
			empBo.updateEmp(employee.getId(), getCurrentEmpNo(), empcontract
					.getEctId());
			addActionMessage("\u7EED\u7B7E\u5458\u5DE5\u5408\u540C\u6210\u529F\u3002");
			return "success";
		}
	}

	public String deleteContract() {
		if (StringUtils.isEmpty(updateEctId)) {
			addErrorInfo("\u8BF7\u9009\u62E9\u8981\u5220\u9664\u7684\u5408\u540C!");
			return "success";
		}
		IEmpContractBo empContractBo = (IEmpContractBo) getBean("empContractBo");
		Empcontract ect = empContractBo.getById(updateEctId);
		if (ect == null) {
			addErrorInfo("\u5408\u540C\u4E0D\u5B58\u5728\u6216\u5DF2\u7ECF\u88AB\u5220\u9664!");
			return "success";
		}
		if ("SUB".equals(super.authorityCondition)
				&& !checkAuth(ect.getEmployee().getId())) {
			addErrorInfo("\u60A8\u6CA1\u6709\u5220\u9664\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "success";
		}
		String fileName = ect.getEctAttatchment();
		if (fileName != null && !"".equals(fileName))
			FileOperate.deleteFile("sys.profile.file.path", fileName);
		empContractBo.delete(ect);
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		Employee emp = empBo.loadEmp(ect.getEmployee().getId(), null);
		List oldList = empContractBo.searchByEmpNo(emp.getId());
		if (oldList != null && oldList.size() > 0)
			emp.setContract((Empcontract) oldList.get(0));
		else
			emp.setContract(null);
		empContractBo.updateObj(emp);
		addSuccessInfo("\u5220\u9664\u5458\u5DE5\u5408\u540C\u6210\u529F\u3002");
		return "success";
	}

	public String updateContract() throws Exception {
		String currentEmpNo;
		IEmpContractBo empContractBo;
		Empcontract empcontract;
		if (StringUtils.isEmpty(employeeId))
			return "error";
		if ("SUB".equals(super.authorityCondition) && !checkAuth(employeeId)) {
			addActionError("\u60A8\u6CA1\u6709\u4FEE\u6539\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		Employee temp = empBo.loadEmp(employeeId, null);
		if (temp.getContract() == null || temp.getContract().getEctId() == null)
			return "error";
		currentEmpNo = getCurrentEmpNo();
		empContractBo = (IEmpContractBo) getBean("empContractBo");
		empcontract = new Empcontract();
		if (fileFileName != null && !"".equals(fileFileName)) {
			String UploadResult;
			String pathConfig = "sys.profile.file.path";
			String typeConfig = "sys.profile.file.type";
			String lengthConfig = "sys.profile.file.length";
			String postfix = fileFileName.substring(fileFileName
					.lastIndexOf("."));
			fileFileName = (new StringBuilder()).append(UUID.randomUUID())
					.append(postfix).toString();
			UploadResult = FileOperate.buildFile(file, pathConfig, typeConfig,
					lengthConfig, new String[] { fileFileName,
							empcontract.getEctAttatchment() });
			if ("property".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u4E0A\u4F20\u9519\u8BEF\uFF01");
				return "error";
			}
			if ("fileExtendNameError".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u540E\u7F00\u540D\u4E0D\u5408\u6CD5\uFF01");
				return "error";
			}
			try {
				empcontract.setEctAttatchment(fileFileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		empcontract.setEctNo(ectNo);
		empcontract.setEctStartDate(ectStartDate);
		if (etcExpire.intValue() != 1)
			empcontract.setEctEndDate(ectEndDate);
		empcontract.setEtcExpire(etcExpire);
		empcontract.setEctComments(ectComments);
		empcontract.setEctStatus(ectStatus);
		empcontract.setEmployee(new Employee(employeeId));
		empcontract.setContractType(new ContractType(empTypeId));
		empcontract.setEctLastChangeBy(currentEmpNo);
		empcontract.setEctLastChangeTime(new Date());
		String error = empContractBo.update(empcontract, updateEctId, null);
		if (StringUtils.isNotEmpty(error))
			addActionError(error);
		else
			addActionMessage("\u66F4\u65B0\u5458\u5DE5\u5408\u540C\u6210\u529F\u3002");
		clear();
		return "success";
	}

	public String attachDelete() throws Exception {
		if (updateEctId == null || updateEctId.equals("")
				|| fileFileName == null || "".equals(fileFileName)) {
			addActionError("\u53C2\u6570\u4F20\u9012\u9519\u8BEF\uFF01");
			return "error";
		}
		IEmpContractBo empContractBo = (IEmpContractBo) getBean("empContractBo");
		Empcontract ect = empContractBo.getById(updateEctId);
		if (ect == null) {
			addActionError("\u53C2\u6570\u4F20\u9012\u9519\u8BEF\uFF01");
			return "error";
		}
		if ("SUB".equals(super.authorityCondition)
				&& !checkAuth(ect.getEmployee().getId())) {
			addActionError("\u60A8\u6CA1\u6709\u4FEE\u6539\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		if (!empContractBo.deleteAttach(updateEctId, fileFileName)) {
			addActionError("\u9644\u4EF6\u5220\u9664\u5931\u8D25\uFF01");
			return "error";
		} else {
			addActionMessage("\u9644\u4EF6\u5220\u9664\u6210\u529F\u3002");
			return "success";
		}
	}

	public String getEctNo() {
		return ectNo;
	}

	public void setEctNo(String ectNo) {
		this.ectNo = ectNo;
	}

	public Date getEctStartDate() {
		return ectStartDate;
	}

	public void setEctStartDate(Date ectStartDate) {
		this.ectStartDate = ectStartDate;
	}

	public Date getEctEndDate() {
		return ectEndDate;
	}

	public void setEctEndDate(Date ectEndDate) {
		this.ectEndDate = ectEndDate;
	}

	public Integer getEtcExpire() {
		return etcExpire;
	}

	public void setEtcExpire(Integer etcExpire) {
		this.etcExpire = etcExpire;
	}

	public String getEctComments() {
		return ectComments;
	}

	public void setEctComments(String ectComments) {
		this.ectComments = ectComments;
	}

	public String getEctStatus() {
		return ectStatus;
	}

	public void setEctStatus(String ectStatus) {
		this.ectStatus = ectStatus;
	}

	public String getEmpTypeId() {
		return empTypeId;
	}

	public void setEmpTypeId(String empTypeId) {
		this.empTypeId = empTypeId;
	}

	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

	public String getEmpNo() {
		return empNo;
	}

	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getUpdateEctId() {
		return updateEctId;
	}

	public void setUpdateEctId(String updateEctId) {
		this.updateEctId = updateEctId;
	}

	public List getContractList() {
		return contractList;
	}

	public void setContractList(List contractList) {
		this.contractList = contractList;
	}

	public List getEmpTypeList() {
		return empTypeList;
	}

	public void setEmpTypeList(List empTypeList) {
		this.empTypeList = empTypeList;
	}

	public String getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}

	public List getDeptList() {
		return deptList;
	}

	public void setDeptList(List deptList) {
		this.deptList = deptList;
	}

	public Date getEctStartDateTo() {
		return ectStartDateTo;
	}

	public void setEctStartDateTo(Date ectStartDateTo) {
		this.ectStartDateTo = ectStartDateTo;
	}

	public Date getEctEndDateTo() {
		return ectEndDateTo;
	}

	public void setEctEndDateTo(Date ectEndDateTo) {
		this.ectEndDateTo = ectEndDateTo;
	}

	private void clear() {
		ectNo = null;
		ectStartDate = null;
		ectEndDate = null;
		ectStartDateTo = null;
		ectEndDateTo = null;
		etcExpire = null;
		fileFileName = null;
		file = null;
		ectComments = null;
		ectStatus = null;
		empTypeId = null;
		employeeId = null;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public String getInput() {
		return input;
	}

	public void setInput(String input) {
		this.input = input;
	}

	public Empcontract getSearchContract() {
		return searchContract;
	}

	public void setSearchContract(Empcontract searchContract) {
		this.searchContract = searchContract;
	}

	public String getContinueEctId() {
		return continueEctId;
	}

	public void setContinueEctId(String continueEctId) {
		this.continueEctId = continueEctId;
	}

	public String getOutmatchModelId() {
		return outmatchModelId;
	}

	public void setOutmatchModelId(String outmatchModelId) {
		this.outmatchModelId = outmatchModelId;
	}

	public String getOutputIoName() {
		return outputIoName;
	}

	public void setOutputIoName(String outputIoName) {
		this.outputIoName = outputIoName;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	private static final long serialVersionUID = 1L;
	private String ectNo;
	private Date ectStartDate;
	private Date ectEndDate;
	private Date ectStartDateTo;
	private Date ectEndDateTo;
	private Integer etcExpire;
	private String fileFileName;
	private File file;
	private String departmentId;
	private String departmentName;
	private String ectComments;
	private String ectStatus;
	private String empTypeId;
	private String employeeId;
	private String empNo;
	private String input;
	private Pager page;
	private String updateEctId;
	private String continueEctId;
	private List contractList;
	private List empTypeList;
	private List deptList;
	private Empcontract searchContract;
	private String outmatchModelId;
	private String outputIoName;
	private String searchOrExport;
}
